package com.example.smartcity_40.house.apter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.Outpatient.HospitaldetailsActivity;
import com.example.smartcity_40.Outpatient.apter.MediclistApter;
import com.example.smartcity_40.Outpatient.bean.MediclistBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.house.HousedetailsActivity;
import com.example.smartcity_40.house.bean.HoustlistBean;
import com.example.smartcity_40.tool.Tool;

import java.util.List;

public class HoustlistApter extends RecyclerView.Adapter<HoustlistApter.Myhot> {

    private Context context;
    private List<HoustlistBean.RowsBean> list;

    public HoustlistApter(Context context) {
        this.context = context;
    }

    public void setDate(List<HoustlistBean.RowsBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public HoustlistApter.Myhot onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.l_item_houselist, null);
        return new HoustlistApter.Myhot(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull HoustlistApter.Myhot holder, int position) {
        try {
            String imgUrl = list.get(position).getPic();
            if (!TextUtils.isEmpty(imgUrl)) {
                Glide.with(context).load(Tool.url + imgUrl)
                        .into(holder.pic);
            }else {
                Glide.with(context).load(Tool.url + imgUrl)
                        .error(R.drawable.fu_img4)
                        .into(holder.pic);
            }

            String serviceName = list.get(position).getSourceName();
            if (!TextUtils.isEmpty(serviceName)) {
                holder.sourceName.setText(serviceName);
            }


            String description = list.get(position).getDescription();
            if (!TextUtils.isEmpty(description)) {
                holder.description.setText(description);
            }
            Integer areaSize = list.get(position).getAreaSize();
            if(areaSize !=null){
                holder.areaSize.setText("面积 :"+areaSize +"m");
            }

            String price = list.get(position).getPrice();
            if (!TextUtils.isEmpty(price)) {
                holder.price.setText("价格: "+price);
            }

            holder.relayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent integer = new Intent(context, HousedetailsActivity.class);
                    integer.putExtra("houseid",String.valueOf(list.get(position).getId()));
                    context.startActivity(integer);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class Myhot extends RecyclerView.ViewHolder {

        private final TextView description;
        private final TextView sourceName;
        private final TextView areaSize;
        private final TextView price;
        private final ImageView pic;
        private final RelativeLayout relayout;

        public Myhot(@NonNull View itemView) {
            super(itemView);
            pic = itemView.findViewById(R.id.pic);
            sourceName = itemView.findViewById(R.id.sourceName);
            description = itemView.findViewById(R.id.description);
            areaSize = itemView.findViewById(R.id.areaSize);
            price = itemView.findViewById(R.id.price);
            relayout = itemView.findViewById(R.id.relayout);
        }
    }
}

